package com.x.ant.manage;

import com.x.ant.dao.DeptDao;
import com.x.ant.dao.UserDao;
import com.x.ant.model.*;
import com.x.ant.util.Base64Image;
import com.x.framework.manage.BaseManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserManage extends BaseManage {
    private final static String STAFF_PHOTO_PATH = "/upload/staff/";

    @Autowired
    private DeptDao deptDao;

    @Autowired
    private UserDao userDao;

    public Staff insertStaff(Staff staff, String deptParentId) throws Exception {
        String userId = staff.getUserId();
        User user = staff.getUser();
        if(staff.getStaffType() == 1) {
            Dept dept = new Dept();
            dept.setDeptLinkmanName(user.getUserName());
            dept.setDeptLinkmanMobile(user.getUserMobile());
            dept.setDeptId(staff.getDeptId());
            this.update(dept);
            this.updateStaffType(staff.getDeptId());
        }
        if(this.isString(userId)) {
            this.update(user);
        } else {
            user.setUserCreateTime(this.getDate(0));
            user = this.insert(user);
            userId = user.getUserId();
        }
        staff.setUserId(userId);

        String staffId = staff.getStaffId();
        String staffPhotoBase64 = staff.getStaffPhoto();
        if(this.isString(staffPhotoBase64)) {
            staff.setStaffPhoto(STAFF_PHOTO_PATH + staffId + ".png");
            Base64Image.generateImage(staffPhotoBase64, getRealPath(STAFF_PHOTO_PATH), staff.getStaffId() + ".png");
        }
        if(this.isString(staffId)) {
            this.update(staff);
        } else {
            staff = this.insert(staff);
        }
        String deptId = staff.getDeptId();
        DeptUser deptUser = new DeptUser();
        deptUser.setDeptId(deptId);
        deptUser.setUserId(userId);
        this.delete(deptUser);
        deptUser.setDeptParentId(deptParentId);
        this.insert(deptUser);
        return staff;
    }

    public Sender insertSender(Sender sender) throws Exception {
        User user = sender.getUser();
        String userId = sender.getUserId();
        if(this.isString(userId)) {
            this.update(user);
        } else {
            user.setUserCreateTime(this.getDate(0));
            user = this.insert(user);
            userId = user.getUserId();
        }
        sender.setUserId(userId);
        String senderId = sender.getSenderId();
        if(this.isString(senderId)) {
            this.update(sender);
        } else {
            sender = this.insert(sender);
        }
        String deptId = sender.getDeptId();
        String deptParentId = sender.getDeptParentId();
        DeptUser deptUser = new DeptUser();
        deptUser.setDeptId(deptId);
        deptUser.setUserId(userId);
        this.delete(deptUser);
        deptUser.setDeptParentId(deptParentId);
        this.insert(deptUser);
        return sender;
    }

    private void updateStaffType(String deptId) throws Exception {
        Staff staffWhere = new Staff();
        staffWhere.setDeptId(deptId);
        staffWhere.setStaffType(1);
        Staff staffSet = new Staff();
        staffSet.setStaffType(0);
        this.update(staffSet, staffWhere);
    }

}